//
//  JEHMoreTableViewCell.m
//  SinaNews
//
//  Created by 蒋恩浩 on 15/12/4.
//  Copyright © 2015年 JEH. All rights reserved.
//

#import "JEHMoreTableViewCell.h"

@interface JEHMoreTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UICollectionViewFlowLayout *layout;
@property (nonatomic, retain) UILabel *subscribeLabel;
@property (nonatomic, retain) UIView *lineView;
@property (nonatomic, retain) JEHDataBaseHandle *handle;
@property (nonatomic, retain) NSMutableArray *nameArray;

@end

@implementation JEHMoreTableViewCell

#warning 会crush（原因不详 MoreVC点订阅的时候）
- (void)dealloc
{
    _delegate = nil;
    [_collectionView release];
    [_layout release];
    [_subscribeLabel release];
    [_lineView release];
    [_subscribeLabel release];
    [_nameArray release];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (void)getNameArrayFromDataBaseHandle {
    
    self.nameArray = [NSMutableArray arrayWithCapacity:0];
    self.handle = [JEHDataBaseHandle shareDataBaseHandle];
    [self.handle openDataBase];
    [self.handle selectTitleArrayAllFromDataBase];
    self.nameArray = self.handle.nameArray.copy;
    [self.handle closeDataBase];
}

#pragma mark - reInitMethod
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createHeaderView];
        [self createCollectionView];
        [self getNameArrayFromDataBaseHandle];
        
#pragma mark - ReloadData
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(receiveNotification:) name:@"ReloadData" object:nil];
    }
    return self;
}

#pragma mark - ReloadData
- (void)receiveNotification:(NSNotification *)notification {
    
    [self getNameArrayFromDataBaseHandle];
    [self.collectionView reloadData];
}

- (void)setTemp:(NSInteger)temp {
    if (_temp != temp) {
        _temp = temp;
    }
}

#pragma mark - layoutSubviews
- (void)layoutSubviews {
    [super layoutSubviews];
    self.layout.itemSize = CGSizeMake((self.width - 50) / 3, 30);
    self.collectionView.frame = CGRectMake(10, 10, self.width - 20, self.height - 80);
    _subscribeLabel.frame = CGRectMake(10, self.height - 20, 70, 20);
    _lineView.frame = CGRectMake(10, self.height - 1, 70, 1);
}

#pragma mark - createHeaderView
- (void)createHeaderView {
    
    self.subscribeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_subscribeLabel release];
    _subscribeLabel.text = @"订阅推荐";
    [_subscribeLabel nightMode];
    self.lineView = [[UIView alloc] initWithFrame:CGRectZero];
    [_lineView release];
    _lineView.backgroundColor = [UIColor redColor];
    [self addSubview:_lineView];
    [self addSubview:_subscribeLabel];
}

#pragma mark - createCollectionView
- (void)createCollectionView {
    
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    [_layout release];
    _layout.minimumLineSpacing = 15;
    _layout.minimumInteritemSpacing = 15;
    _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    [_collectionView release];
    [_layout release];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView nightMode];
    [_collectionView registerClass:[JEHMoreCollectionViewCell class] forCellWithReuseIdentifier:@"JEHMoreCollectionViewCellIdentifier"];
    [self addSubview:self.collectionView];
}

#pragma mark - UICollectionViewDelegateMethod
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.nameArray.count + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    JEHMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JEHMoreCollectionViewCellIdentifier" forIndexPath:indexPath];
    if (indexPath.item < self.nameArray.count) {
        
        cell.titleLabel.text = self.nameArray[indexPath.item];
    } else if (indexPath.item == self.nameArray.count) {

        cell.titleLabel.text = @"+";
#warning 点击订阅颜色会乱变，未解决1
//        cell.titleLabel.textColor = [UIColor grayColor];
    }
#warning 点击订阅颜色会乱变，未解决2
//    if (indexPath.item == _temp) {
//
//        cell.titleLabel.textColor = [UIColor redColor];
//    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.item == self.nameArray.count) {
        
        [self.delegate pushToNextPage];
    } else {
        [self.delegate changeContentOfSetWithPage:indexPath.item];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
